<?php

session_start();

include_once "src/controller/blmanagement/ReservationManagement.php";
include_once "src/controller/blmanagement/UserAccountManagement.php";
include_once "src/model/Reservation.php";
include_once "src/model/User.php";
include_once "src/misc/ClientReponseGenerator.php";

try {

    $user = UserAccountManagement::getUserByEmail($_SESSION["email"]);

    $reservation = new Reservation();
    $reservation->setResourceID($_POST["addResourceReservationId"]);
    $reservation->setUserID($user->getUserId());
    $reservation->setStartDate($_POST["addResourceReservationStartDate"]);
    $reservation->setEndDate($_POST["addResourceReservationEndDate"]);
    

    if($_POST["addResourceReservationReoccurrenceId"] != "0") {
        $reservation->setReOccurrenceID($_POST["addResourceReservationReoccurrenceId"]);
    }

    if($_POST["addResourceReservationStopDate"] != "") {
        $reservation->setStopDate($_POST["addResourceReservationStopDate"]);
    }

    if (ReservationManagement::addReservation($reservation)) {
        echo ClientReponseGenerator::simpleJsonResponse(1, "", "");
    } else {
        throw new Exception();
    }
    
    sleep(1);
} catch (Exception $e) {
    echo ClientReponseGenerator::simpleJsonResponse(0, "AddReservationException", "");
}

?>
